<?php

abstract class Dcmp_Model_Status extends Dcmp_Model_Spoor {

    public function __construct() {
        parent::__construct();
    }

    public function delete($transaction = NULL) {
        $transaction = is_null($transaction) ? Kd_Db_Factory::getTransaction() : $transaction;
        try {
            $transaction->begin();
            $this->beforeDelete($transaction);
            $idColumn = $this->getTable()->getIdColumn();
            Kd_Data_Factory::getUpdateDcl()
                ->into($this->getTable()->getName())
                ->value('status', "'deleted'")
                ->where()
                ->eq($idColumn->getColumnName(), $idColumn->getParameterName())
                ->closest()
                ->addParameter($idColumn->getColumnName(), $this->getId())
                ->setDbDriver($transaction->getDbDriver())
                ->execute();
            $this->beforeUpdate($transaction);
            $transaction->commit();
        } catch (Exception $ex) {
            $transaction->rollback();
            throw $ex;
        }

        return $this;
    }

    public function deleteBatch(array $ids, $transaction = NULL) {
        $transaction = is_null($transaction) ? Kd_Db_Factory::getTransaction() : $transaction;
        try {
            $transaction->begin();
            $idColumn = $this->getTable()->getIdColumn();
            Kd_Data_Factory::getUpdateDcl()
                ->into($this->getTable()->getName())
                ->value('status', "'deleted'")
                ->where()
                ->contain($idColumn->getColumnName(), $idColumn->getParameterName())
                ->closest()
                ->addParameter($idColumn->getColumnName(), $ids)
                ->setDbDriver($transaction->getDbDriver())
                ->execute();
            $transaction->commit();
        } catch (Exception $ex) {
            $transaction->rollback();
            throw $ex;
        }

        return $this;
    }

    public function updateStatus($status, array $ids, $message = NULL, $statusFilter = array()) {
        $update = Kd_Data_Factory::getUpdateDcl()
            ->into($this->getTable()->getName())
            ->value('status', ':status:s')
            ->value('mender_id', Kd_Web::getIdentity()->getId())
            ->value('modified_time', ":modifiedTime:s")
            ->value('status_message', ':statusMessage:s')
            ->where()
                ->contain($this->getIdProperty(), $this->getTable()->getIdColumn()->getParameterName())
            ->closest();
        if(!empty($statusFilter)){
            $update->where()->contain('status', ':statusFilter:s')->closest()
                   ->addParameter('statusFilter', $statusFilter);
        }
        $update->addParameter(array($this->getIdProperty()=>$ids,
                'modifiedTime'=>date('Y-m-d H:i:s'),
                'status'=>$status,
                'statusMessage'=>$message))
            ->execute();
        return $this;
    }

    public function publish(array $ids, $message = NULL){
        return $this->updateStatus('published', $ids, $message, array('pending', 'forbidden'));
    }

    public function forbid(array $ids, $message = NULL){
        return $this->updateStatus('forbidden', $ids, $message, array('published', 'pending'));
    }

    public function isPublished(){
        return $this->getStatus() == 'published';
    }

    public function isDraft(){
        return $this->getStatus() == 'draft';
    }

    public function isPending(){
        return $this->getStatus() == 'pending';
    }

    public function isForbidden(){
        return $this->getStatus() == 'forbidden';
    }

    protected function initializeTable($table) {
        $this->getTable()->createColumn('status', 'string');
        $this->getTable()->createColumn('status_message', 'string');
        parent::initializeTable($table);
    }

}